package pro.cherish.knowledgeumldesignpattern.designPattern.com.cherish.prototype;

// 运行结果
// 耗时10977
//耗时10
//        结论就是：如果new消耗大，则可以考虑原型模式
//效率差异比较大
//原型模式可以和工厂模式结合起来，通过clone一个对象后，将对象由工厂提供给调用者。
//spring 的bean的创建实际就是两种：单例模式和原型模式（当然，原型模式需要和工厂模式结合起来）
public class client4 {
    public static void main(String [] args) throws InterruptedException, CloneNotSupportedException {
        long start = System.currentTimeMillis();
        testNew(1000);
        testClone(1000);
        long end = System.currentTimeMillis();
    }

    public static void testNew(int size) throws InterruptedException {
        long start = System.currentTimeMillis();
        for (int i=0;i<size;i++){
            Laptop laptop = new Laptop();
        }
        long end = System.currentTimeMillis();
        System.out.println("耗时"+(end-start));
    }

    public static void testClone(int size) throws InterruptedException, CloneNotSupportedException {
        long start = System.currentTimeMillis();
        Laptop laptop = new Laptop();
        for (int i=0;i<size-1;i++){
            Object clone = laptop.clone();
        }
        long end = System.currentTimeMillis();
        System.out.println("耗时"+(end-start));
    }
}
//笔记本电脑
class  Laptop implements Cloneable{
    public Laptop() throws InterruptedException {
//        模拟创建对象耗时场景
        Thread.sleep(10);
    }
    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}
